package com.deliveryservicesystem.presentation.desktop;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 * File Name: GenericTableModel.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.presentation.desktop
 * Author: Lance
 * Date: Jun 19, 2012
 * Editor:
 */
public class GenericTableModel
{

    /**
     * Converts a ResultSet to a TableModel
     *
     * @param resultSet to be converted to a TableModel
     * @return TableModel that has been converted
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public static TableModel resultSetToTableModel(ResultSet resultSet) throws SQLException
    {
        ResultSetMetaData metaData = resultSet.getMetaData();
        int numberOfColumns = metaData.getColumnCount();

        Vector columnNames = new Vector();
        for (int column = 0; column < numberOfColumns; column++)
        {
            String columnLabel = metaData.getColumnLabel(column + 1);
            String replace = columnLabel.replaceAll("_", " ");
            String replaceAll = replace.replaceAll("NO", "NUMBER");
            columnNames.addElement(replaceAll);
        }

        Vector rows = new Vector();
        while (resultSet.next())
        {
            Vector newRow = new Vector();
            for (int i = 1; i <= numberOfColumns; i++)
            {
                try
                {
                    Date date = resultSet.getDate(i);
                    String format = new SimpleDateFormat("dd MMM yyyy").format(date);
                    newRow.addElement(format);
                }
                catch (Exception e)
                {
                    try
                    {
                        BigDecimal bigDecimal = resultSet.getBigDecimal(i);

                        if (bigDecimal.equals(BigDecimal.ONE))
                        {
                            newRow.addElement("Yes");
                        }
                        else if (bigDecimal.equals(BigDecimal.ZERO))
                        {
                            newRow.addElement("No");
                        }
                        else
                        {
                            throw e;
                        }
                    }
                    catch (Exception e1)
                    {
                        Object o = resultSet.getObject(i);
                        if (o == null)
                        {
                            newRow.addElement("0");
                        }
                        else
                        {
                            newRow.addElement(o);
                        }
                    }
                }
            }
            rows.addElement(newRow);
        }

        return new DefaultTableModel(rows, columnNames);
    }
}